//
//  oled25.metal
//  videoPlayerSwift
//
//  Created by zhe wu on 2025/11/24.
//  Copyright © 2025 xuwanliang. All rights reserved.
//
#include <metal_stdlib>
using namespace metal;

#include "ExampleShaderParams.h"



kernel void oled25(texture2d<half, access::write> outTexture [[texture(0)]],
                   texture2d<half, access::sample> colorTexture [[texture(1)]],
                   sampler colorSampler [[sampler(0)]],  // 新增：采样器，用于过滤
                   constant ExampleShaderParams& params [[buffer(0)]],
                   uint2 gid [[thread_position_in_grid]])
{
    // Get texture dimensions
    uint2 dims = uint2(outTexture.get_width(), outTexture.get_height());
    
    // Convert pixel position to -1 to 1 range
    float2 uv = float2(gid) / float2(dims) * 2.0 - 1.0;
    
    // Calculate distance from center
    float dist = length(uv);
    
    // Create animated rings
    float ring = sin(dist * 10.0 - params.time * 2.0) * 0.5 + 0.5;
    
    // Create a color pattern that varies with position and time
    float3 color;
    color.r = ring * (sin(params.time) * 0.5 + 0.5);
    color.g = ring * (sin(params.time + 2.094) * 0.5 + 0.5);
    color.b = ring * (sin(params.time + 4.189) * 0.5 + 0.5);
    
    //color.r = gid.x % 2;
    color.g = 0.0;
    color.b = 0.0;
    
    float3 i;
    float3 intensity_bias;
    float hpitch = 2.8;
    float vpitch = 1.8;
    float u_view = 2.0;
    float scw = 2868.0;
    float sch = 1320.0;
    const float blk_lv = 0.09682806;
    const float white_lv = 1.121224;
    const float gamma_comp = 1.102778;
    const float r_comp_p = 0.1116109;
    const float g_comp_p = 0.1116109;
    const float b_comp_p = 0.1116109;
    const float r_comp_n = 0.02884828;
    const float g_comp_n = 0.02884828;
    const float b_comp_n = 0.02884828;
    const float nokoCoef = 1.075318;
    const float nokoBias = 0.0;
    const float nokoPow = 1.299941;
    
    float halfScreenWidth = scw/2.0;
    float halfScreenHeight = sch/2.0;
    float pitch = abs(hpitch);
    float tan_theta = hpitch / vpitch;

    float Xc = gid.x + u_view - halfScreenWidth + (pitch / 4.0);
    float Yc = (sch - 1.0 - gid.y) - halfScreenHeight;
    
    float RX = Xc;
    float RY = Yc;
    
    float DistFromStart;
    float SubPitchsFromStart;
    int theViewR, theViewG, theViewB;

    float half_pitch = pitch / 2.0;
    float nokoriDist;

    DistFromStart = RX - RY * tan_theta;
    SubPitchsFromStart = (DistFromStart / half_pitch);
    theViewR = int(fmod(SubPitchsFromStart, 2.0));
    nokoriDist = DistFromStart - (floor(SubPitchsFromStart) * half_pitch);
    nokoriDist = nokoriDist / half_pitch;  // 位於帶寬的百分比

    nokoriDist = clamp(nokoriDist, 0.0, 1.0);
    i.r = (theViewR == 0) ? 0.0 : 1.0;
    intensity_bias.r = 1.0 - (abs(nokoriDist - 0.5) * 2.0);
    
    intensity_bias.r = nokoCoef * intensity_bias.r + nokoBias;
    intensity_bias.r = (pow(intensity_bias.r, nokoPow));

    intensity_bias = clamp(intensity_bias, 0.0, 1.0);
    
    float r = blk_lv + pow(1.0, gamma_comp) * (white_lv - blk_lv);
    float r_inv = blk_lv + pow(0.0, gamma_comp) * (white_lv - blk_lv);
    
    float r_comp;
    if(r < r_inv){
        r_comp = r_comp_p;
    } else{
        r_comp = r_comp_n;
    }

    float output_r = r + (r - r_inv) * r_comp;
    color.r = output_r * intensity_bias.r;
    
    
    float2 texCoor;
    texCoor.x = gid.x/scw;
    texCoor.y = gid.y/sch;
    half4 coll = colorTexture.sample(colorSampler, texCoor);
    // Convert to half3 for output
    //outTexture.write(half4(half3(color), 1.0), gid);
    outTexture.write(coll, gid);
}

